package com.easy.facilitaterecycleadapter.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Sigthp-P-Department on 2018/5/4.
 */

public class CustomGrideLayoutDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDrawable;
    private boolean lastCulmer;
    private boolean lastCulme;
    private int spanCount;

    public CustomGrideLayoutDecoration(Drawable drawable){
        mDrawable=drawable;
    }
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawHorizongtor(c,parent,state);
        drawVertical(c,parent,state);
    }

    private void drawVertical(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int itemCount = state.getItemCount();
        Rect rect=new Rect();
        for(int x=0;x<itemCount;x++){
            if((x+1) % spanCount != 0){
                View childAt = parent.getChildAt(x);
                rect.left=childAt.getRight();
                rect.right=childAt.getRight()+mDrawable.getIntrinsicWidth();
                rect.top=childAt.getTop();
                rect.bottom=childAt.getBottom();
                mDrawable.setBounds(rect);
                mDrawable.draw(c);
            }
        }
    }

    private void drawHorizongtor(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int itemCount = state.getItemCount();
        Rect rect=new Rect();

        for(int x=0;x<itemCount;x++){
            if((x+1)<=(itemCount-spanCount+1)){
                View childAt = parent.getChildAt(x);
                rect.left=childAt.getLeft();
                rect.right=childAt.getRight()+mDrawable.getIntrinsicWidth();
                rect.top=childAt.getBottom();
                rect.bottom=rect.top+mDrawable.getIntrinsicHeight();
                mDrawable.setBounds(rect);
                mDrawable.draw(c);
            }

        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int currentPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        int bottom= mDrawable.getIntrinsicHeight();
        int right = mDrawable.getIntrinsicWidth();

        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            spanCount = gridLayoutManager.getSpanCount();
            if (isLastCulme(currentPosition, spanCount)) {
                right = 0;
            }


            if (isLastRow(spanCount, parent, currentPosition)) {
                bottom = 0;
            }
            outRect.bottom = bottom;
            outRect.right = right;

        } else {
            System.out.println("不是GridLayout");
        }

    }

    public boolean isLastRow(int spanCount, RecyclerView parent, int currentPosition) {
        int rowNumber = parent.getAdapter().getItemCount() % spanCount == 0 ?
                parent.getAdapter().getItemCount() / spanCount : (parent.getAdapter().getItemCount() / spanCount + 1);
        return (currentPosition+1)>(rowNumber-1)*spanCount;
    }

    public boolean isLastCulme(int currentPosition,int spanCount) {
        return (currentPosition + 1) % spanCount == 0;
    }
}
